<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/treetable/jquery.treetable.css"/>
    <link rel="stylesheet" href="/css/treetable/jquery.treetable.theme.default.css"/>
    <script src="/js/libs/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
    <script src="/js/bootstrap/bootstrap.min.js"></script>
    <script src="/js/bootstrap/bootstrap-select.min.js"></script>
    <script src="/js/jq.js"></script>
    <script src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/layui/layui.js"></script>
    <script src="/js/dict.js"></script>
    <script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
    <script src="/js/my/ztree-menu.js"></script>
    <script src="/js/my/permission.js"></script>
    <script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
    <script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
    <script src="/js/libs/jquery.treetable.js"></script>
</head>
<body>
<div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <div>
            <header style="height: 100%">
                <div align="left">
                    <table style="width: 100%">
                        <tr>
                            <td>
                                <form id="form" class="form-inline" method="post" action="/order/export"
                                      enctype="multipart/form-data">
                                    <div class="form-group">
                                        <label style="float: left;"> 部门：</label>
                                        <input class='form-control' type='hidden' name='originatorOfficeId'
                                               id='originatorOfficeId'>
                                        <input style="width: 10%;float: left;" class='form-control' placeholder='部门'
                                               type='text' name='originatorOfficeName' id='originatorOfficeName'
                                               readonly>
                                        <a href="#" style="width: 5%;float: left; height: 32px; line-height: 32px;"
                                                class="layui-btn layui-btn-sm"
                                                onclick="showSpecialTree('originatorOfficeId', 'originatorOfficeName', 'dept', '部门', 'root', 'select','fasDeptTree');return false;">
                                            <i class="layui-icon">&#xe615;</i>
                                        </a>
                                        &nbsp; &nbsp;
                                        <label>公司：</label>
                                        <select class="form-control input-sm col-md-1 selectpicker show-tick" name="companyId"
                                                id="companyId"
                                                title="公司" data-live-search="true">
                                        </select>
                                        &nbsp; &nbsp;
                                        <label>需求单状态：</label>
                                        <select class="form-control input-sm col-md-1 selectpicker show-tick" name="orderType"
                                                id="orderType"
                                                title="需求单状态" data-live-search="true">
                                        </select>
                                        &nbsp; &nbsp;
                                        <label>创建时间：</label>
                                        <input type="text" class="form-control" placeholder="开始时间" id="beginTime">
                                        <input type="text" class="form-control" placeholder="结束时间" id="endTime">
                                        &nbsp; &nbsp;
                                    </div>
                                </form>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <button id="searchBt" class="layui-btn layui-btn-sm search"><i class="layui-icon">&#xe615;</i>搜索
                                </button>
                            </td>
                            <td align="right">
                                <iframe id="export" style="display:none"></iframe>
                                <button class='layui-btn layui-btn-sm' permission="fas:order:export"
                                        onclick="exportData()">
                                    <i class='layui-icon'>&#xe601;</i> 导出
                                </button>
                            </td>
                        </tr>
                    </table>
                </div>
            </header>

            <div>
                <div class="widget-body no-padding">
                    <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                        <thead>
                        <tr>
                        </tr>
                        <tr>
                            <th>编号</th>
                            <th>需求部门</th>
                            <th>公司名称</th>
                            <th>需求单状态</th>
                            <th>期望交付时间</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    var pers = checkPermission();
    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#beginTime'
        });
        laydate.render({
            elem: '#endTime'
        });
    });
    $(function () {
        showDictSelect('companyId', 'company', '--请选择--', '');
        showDictSelect('orderType', 'orderType', '--请选择--', '');
    });
    var example;

    function init() {
        example =
            $('#dt-table').DataTable({
                "searching": false,
                "processing": false,
                "serverSide": true,
                "language": {
                    "url": "/js/plugin/datatables/Chinese.lang"
                },
                "ajax": {
                    "url": "/order/list",
                    "type": "get",
                    "data": function (d) {
                        d.originatorOfficeId = $('#originatorOfficeId').val().trim();
                        d.companyId = $('#companyId').val().trim();
                        d.beginTime = $("#beginTime").val();
                        d.endTime = $("#endTime").val();
                        d.orderType = $("#orderType").val();
                    }
                },
                "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",
                "columns": [
                    {"data": "name", "defaultContent": ""},
                    {"data": "originatorOfficeName", "defaultContent": ""},
                    {"data": "companyName", "defaultContent": ""},
                    {
                        "data": "orderType", "defaultContent": ""
                        , render: function (data, type, row) {
                            return getDict('buy_status')[data];
                        }
                    },
                    {"data": "wishPayDate", "defaultContent": ""},
                    {"data": "createTime", "defaultContent": ""},
                    {
                        "data": "",
                        "defaultContent": "",
                        "orderable": false,
                        "render": function (data, type, row) {
                            var id = row['id'];
                            var orderType = row['orderType']
                            var href = "updateOrder.html?id=" + id;
                            var edit = "", del = "", submitBtn = "", withdrawBtn = "", confirmBtn = "", rejectBtn = "";
                            if (orderType == 0) {
                                edit = buttonEdit(href, "fas:order:edit", pers);
                                del = buttonDel(id, "fas:order:del", pers);
                                //提交 撤回 确认 驳回 交付等按钮..
                                submitBtn = buttonOther(id, "submit", "提交", "fas:order:submit", pers);
                            } else if (orderType == 1) {
                                withdrawBtn = buttonOther(id, "withdraw", "撤回", "fas:order:withdraw", pers);
                                rejectBtn = buttonOther(id, "reject", "驳回", "fas:order:reject", pers);
                                confirmBtn = buttonOther(id, "confirm", "审批通过", "fas:order:confirm", pers);
                                edit = buttonEdit(href, "fas:order:edit", pers);
                            } else {
                                edit = buttonEdit(href, "fas:order:edit", pers);
                                del = buttonDel(id, "fas:order:del", pers);
                            }
                            return edit + del + submitBtn + withdrawBtn + confirmBtn + rejectBtn;
                        }
                    },

                ],
                "order": [[5, "desc"]]
            });
    }

    layui.use('layer', function () {
        var layer = layui.layer;
    });

    //提交审批
    function approval(id, type) {
        layer.confirm('确定要提交吗？', {
            btn: ['确定', '取消']
        }, function () {
            var uri = "/order/approval";
            $.ajax({
                type: 'post',
                url: uri,
                data: {'id': id, 'type': type},
                success: function (data) {
                    example.ajax.reload();
                    layer.msg("操作成功");
                }
            });
            layer.close(1);
        });
    }

    //生成审批按钮的方法
    function buttonOther(id, type, title, permission, pers) {
        if (permission != "") {
            if ($.inArray(permission, pers) < 0) {
                return "";
            }
        }
        // var btn = $("<button class='layui-btn layui-btn-xs' title='编辑' onclick='window.location=\"" + href +"\"'><i class='layui-icon'>&#xe642;</i></button>");
        var btn = $("<button class='layui-btn layui-btn-xs' title='" + title + "' onclick=\"approval('" + id + "','" + type + "')\">" + title + "</button>");

        return btn.prop("outerHTML");
    }

    function del(id) {
        layer.confirm('确定要删除吗？', {
            btn: ['确定', '取消']
        }, function () {
            $.ajax({
                type: 'delete',
                url: '/order/' + id,
                success: function (data) {
                    example.ajax.reload();
                    layer.msg("删除成功");
                }
            });
            layer.close(1);
        });
    }

    $("#searchBt").click(function () {
        example.ajax.reload();
    });

    init();

    // 导出
    function exportData() {
        layer.confirm('请选择要导出的方式', {
            btn: ['导出数据', '导出空白模板'] //按钮
        }, function (index) {
            $('#form').attr('action', '/order/export/1').submit();
            layer.close(index);
        }, function () {
            $('#form').attr('action', '/order/export/0').submit();
        });
    }
</script>
